package br.siseventos.view.conversores;

import br.siseventos.model.TbEvento;
import br.siseventos.service.EventoService;
import br.siseventos.view.util.MessageHelper;
import br.siseventos.view.util.ScheduleEventConverter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import org.primefaces.model.LazyScheduleModel;
import org.primefaces.model.ScheduleEvent;

public class CustomScheduleModel extends LazyScheduleModel implements Serializable {

    // <editor-fold desc="Campos">
    private EventoService eventoService = new EventoService();
    private List<TbEvento> eventosPojo = new ArrayList<TbEvento>();
    private List<ScheduleEvent> eventosSchedule = new ArrayList<ScheduleEvent>();
    // </editor-fold>
    // <editor-fold desc="Construtor">
    public CustomScheduleModel() {
    }
    // </editor-fold>
    // <editor-fold desc="Sobrescritas">
    @Override
    public void loadEvents(Date start, Date end) {
        // Limpando os eventos
        clear();

        try {
            // Buscando os eventos
            eventosPojo = eventoService.consultar(start, end);
            eventosSchedule = ScheduleEventConverter.convertToScheduleEvents(eventosPojo);

            // Adicionando os eventos ao ScheduleModel
            getEvents().addAll(eventosSchedule);
        } catch (Exception e) {
            MessageHelper.showGlobalMessage("Ocorreu um erro ao pesquisar os eventos!", FacesMessage.SEVERITY_ERROR);
        }
    }
    
    // </editor-fold>
}
